<?php
/**
 * ENM Web Crawling : Step 1 : Employee List
 */

use Custom\Student;
use Fox\Common\Localization\SubRegion;
use Fox\Common\Localization\City;
use Fox\Common\Localization\City\Street;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Fox\Html\Screen;

set_time_limit(Xview::TIMEOUT_VERY_LONG);

$screen = new Screen("extract","extract");
$block = new Block("b","Extract Source Files");
$block->add(new TextArea("comments",false));
$block->add(new CheckBox("commit"));
//$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		// Load Files 
		
		/*
		for($i="A";$i != "AA";$i++)
		{
			for($j="A";$j != "AA";$j++)
			{	
				$letters = $i.$j;
		*/
		$dir = _TEMP_."ENMSE/V2/Treated/";
		if ($dh = opendir($dir)) 
		{
			$exit = true;
			while (($file = readdir($dh)) !== false && $exit) 
			{
				if(filetype($dir.$file) != 'file')
				{
					continue;
				}					
				$letters = substr($file,0,4);
				// Open File
				$ficnm = _TEMP_."ENMSE/V2/Treated/".$letters.".htm";
				var_dump($ficnm);
				if(!file_exists($ficnm))
					continue;

				// Extract Content 
				$html = file_get_html($ficnm);
				$blockquote = $html->find("form[name=maj] blockquote p font b");
				$rows = 0;
				if(isset($blockquote[0]))
				{
					$rows = (int) trim($blockquote[0]->innertext);
				}
				if(!($rows > 0))
				{
					push_msg("E","No result found for $letters ");
					unlink($ficnm);
					continue;
				}
				elseif($rows >= 200)
				{
					push_msg("W","More than 200 results for $letters ");
					rename($ficnm,_TEMP_."ENMSE/V2/More/".$letters.".htm");
					continue;
				}
				
				$k = 1;
				$error = false;
				$lines = $html->find("form[name=maj] table[width=580] tr");
				for($r=1;$r < count($lines);$r++)
				{
					$line = $lines[$r];
					$fields = null;
					// Find detail
					$col1 = $line->find("td[width=21]");
					$cell1 = trim($col1[0]->innertext);
					if(!empty($cell1))
					{
						$fields['status'] = 9;//No information

					}
					// Find First Name
					$col2 = $line->find("td[width=92] font");
					if(!isset($col2[0]))
					{
						$test = $line->find("td[width=92]");
						push_msg("W","Empty line in $letters ");
						continue;
					}
					$fields['lname'] = trim($col2[0]->innertext);
					// Find Last Name
					$col3 = $line->find("td[width=102] font");
					$fields['fname'] = trim($col3[0]->innertext);
					// Find Reference
					$col4 = $line->find("td[width=85] font");
					$fields['cell4'] = trim($col4[0]->innertext);
					// Find Reference
					$col5 = $line->find("td[width=239] em");
					if(isset($col5[0]))
						$fields['cell5'] = trim($col5[0]->innertext);
					else
						$fields['cell5'] = null;
					// Find Reference
					$fields['cell6'] = null;
					$col6 = $line->find("td[width=22] img");
					if(isset($col6[0]))
					{
						$data = null;
						preg_match("/det\=(.+)\',\'fenfiche/",trim($col6[0]->onclick),$data);
						if(isset($data[1]))
						{
							$fields['reference'] = @$data[1];
							$fields['status'] = 0;
						}
						else
						{
							$fields['reference'] = "ER";
							$fields['status'] = 8;
						}
					}
					elseif(!isset($fields['status']))
					{
						$fields['status'] = 7;
					}
					
					if($fields['status'] == 9 || $fields['status'] == 7)
					{
					
						
					}
					$finder = new Student\Enm();
					
					if(isset($fields['reference']))
					{
						$res = $finder->find(new Fox\Dao\SqlFragment("reference = ? ",array($fields['reference'])));
						if(count($res) > 0)
						{
							push_msg("W","Duplicate entry for {$fields['fname']} {$fields['lname']} ");
							$k++;
							continue;
						}
						$content = '"'.$fields['fname'].'";"'.$fields['lname'].'";"'.$fields['status'].'";"'.$fields['reference']."\n\r";
						file_put_contents(_TEMP_."ENMSE/final.txt", $content,FILE_APPEND);
						$k++;
					}
					else
					{
						$res = $finder->find(new Fox\Dao\SqlFragment("fname = ? AND lname = ? ",array($fields['fname'],$fields['lname'])));
						if(count($res) > 0)
						{
							push_msg("W","Duplicate entry for {$fields['fname']} {$fields['lname']} ");
							$k++;
							continue;
						}
					}
					
					try {
						
						$employee = new Student\Enm();
						$employee->setFields($fields);
						if($inputs['commit'])
						{
							// Save in the DB
							$employee->create();
						}
						push_msg("I","Students {$fields['fname']} has been created $k / $rows ");
					}
					catch(Fox\Object\Exception\Save $e)
					{
						var_dump($e);
						push_msg("E",$e->getMessage());
					}
					
					
				}
			
				if(!$error)
				{
					rename($ficnm,_TEMP_."ENMSE/Treated/".$letters.".htm");
				}
				else
				{
					//rename($ficnm,_TEMP_."ENMSE/V2/Errors/".$letters.".htm");
				}
				
				//$exit = false;
				
				//exit;
			}
        closedir($dh);
		}
	}
}
echo $screen->display();